home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7821 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  59 lines

  1. Path: news.clinet.fi!usenet
  2. From: tkassila@pcuf.fi (Tuomas Kassila)
  3. Newsgroups: comp.lang.c++
  4. Subject: istrstream var(char*) and var >> number?
  5. Date: 19 Feb 1996 14:04:11 GMT
  6. Organization: Clinet, Espoo, Finland.
  7. Message-ID: <4ga00r$mln@news.clinet.fi>
  8. NNTP-Posting-Host: pcuf.fi
  9.  
  10. /* 
  11. Can somebody answer, why out-commented lines not work with 
  12. B4.53/ANSI C++ instead of line marked with #2 version? 
  13. And is there an another (way with istrstream-variable), wich works? 
  14. Here is my little program, which reads stdin. An input is like:
  15.  
  16.       some row 1
  17.       233 Messageheader
  18.    <end of file>
  19.  
  20. and output is:
  21.  
  22.       some row 1
  23.       review 233
  24.    <end of file>
  25.  
  26. If I run the program with version 1 lines (version 2-line is then 
  27. comment line), output will be like:
  28.  
  29.       some row 1
  30.       233 Messageheader
  31.    <end of file>
  32. */ 
  33.  
  34. #include <iostream.h>
  35. #include <strstrea.h>
  36. #define LINELEN 100
  37.  
  38. main()
  39. {
  40. unsigned long msgnumber = 0;
  41. char line[LINELEN+1];
  42. // istrstream str(line); // #1 version
  43.  
  44. *line = '\0';
  45. while (cin.getline(line, LINELEN))
  46. {
  47. // see my question above:
  48. istrstream str(line); // this #2 version works = ok!
  49. if (str >> msgnumber )
  50. cout << "review " << msgnumber << endl;
  51. else
  52. cout << line << endl;
  53. //str.seekg(0L, ios::beg); // #1 version
  54. }
  55. return 0;
  56. } // end of program (and the message)
  57. // --------------------------------------------------------------
  58. // Thanks, Tuomas
  59.